package com.jjh.service.impl;

import com.jjh.dao.impl.UserDaoImpl;
import com.jjh.winterframework.annatation.Component;
import com.jjh.dao.UserDao;
import com.jjh.pojo.User;
import com.jjh.service.UserSerice;
import com.jjh.util.FileUtils;
import com.jjh.util.RandomUtils;
import com.jjh.Application;
import com.jjh.web.Menu;
import com.jjh.winterframework.annatation.Autowired;
import com.jjh.wrapper.QueryWrapper;

import java.util.*;

/**
 * 用户服务类
 * @author JJH
 */
@Component
public class UserServiceImpl implements UserSerice {

    private static Scanner scanner =new Scanner(System.in);
    private static final String path = "E:\\EeleMa\\src\\main\\resources\\users.txt";

    @Autowired
    private UserDaoImpl userDao ;

    /**
     * 用户登录
     * @return
     */
    @Override
    public boolean login() {

        while (true){
            System.out.print("请输入用户名:");
            String username = scanner.nextLine();
            List<Object> userList = FileUtils.fileToList(path, User.class);

            boolean flag = false;
            for (Object obj : userList) {
                User user = (User) obj;
                if(username.equals(user.getUsername())){
                    flag = true;
                    try {
                        wirtePassword(user);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                }
            }

            if(flag==false){
                //没找到相等的用户名，说明没有该用户，需要注册
                System.out.println("=====>没有该用户名! 请注册:");
                System.out.println();
                Menu.show();
            }

            break;
        }
        return true;
    }
    //填写密码
    private void wirtePassword(User user) throws InterruptedException {
        while (true){
            System.out.print("请输入密码：");
            String password = scanner.nextLine();
            if(password.equals(user.getPassword())){
                //密码正确，可以登录了，跳转到功能界面
                //把用户放到当前线程中
                Application.threadLocal.set(user);
                System.out.println("======>正确！ 正在进入系统...");
                Thread.sleep(500);
                  Menu.subMenu01();
                  break;
            }else{
                System.out.println("====>密码错误！请重新输入:");
            }
        }
    }


    /**
     * 用户注册
     * @return
     */
    @Override
    public boolean register(){


            System.out.println("请输入用户名:");
            String username = scanner.nextLine();
            String password1;
            while (true) {
                System.out.println("请输入密码:");
                password1 = scanner.nextLine();
                System.out.println("请再次确认密码:");
                String password2 = scanner.nextLine();
                if (password2.equals(password1)) {
                    break;
                } else {
                    System.out.println("两次输入的密码不一样，请重新输入密码:");
                }
            }
                System.out.println("请输入年龄:");
                int age = scanner.nextInt();

                scanner.nextLine();

                System.out.println("请输入手机号:");
                String phone = scanner.nextLine();
                System.out.println("请输入收货地址:");
                String address = scanner.nextLine();

                User user = new User();
                user.setId(RandomUtils.getId());
                user.setUsername(username);
                user.setPassword(password1);
                user.setAge(age);
                user.setPhone(phone);
                user.setAddress(address);

                FileUtils.ListToFile(List.of(user),path,User.class);
                //把用户放入当前线程中
                Application.threadLocal.set(user);
                System.out.println("注册成功！正在进入系统...");
        try {
            Thread.sleep(1000);

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    /**
     * 根据用户id获取地址
     * @param id
     * @return
     */
    @Override
    public String getAddress(int id) {
        return userDao.getAddressById(id);
    }

    /**
     * 根据条件获取用户
     * @return
     */
    @Override
    public User getUser(QueryWrapper wrapper){
        return userDao.getOne(wrapper);
    }

    /**
     * 根据主键修改用户
     * @param user
     * @return
     */
    @Override
    public boolean updateOne(User user) {
       return userDao.updateOne(user);
    }


}

